package com.xiaoshuidi.cloud.module.member.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.member.controller.admin.enterprise.vo.EnterpriseInfoRespVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.enterprise.vo.EnterprisePageReqVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.enterprise.vo.EnterpriseVO;
import com.xiaoshuidi.cloud.module.member.controller.app.enterprise.vo.EnterpriseCreateVO;
import com.xiaoshuidi.cloud.module.member.controller.app.user.vo.MemberVO;
import com.xiaoshuidi.cloud.module.member.mapper.enterprise.MemberEnterpriseMapper;
import com.xiaoshuidi.cloud.module.member.pojo.enterprise.MemberEnterprise;
import com.xiaoshuidi.cloud.module.member.pojo.user.MemberUser;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor
public class MemberEnterpriseRepository extends ServiceImpl<MemberEnterpriseMapper, MemberEnterprise> {
    private final MemberEnterpriseMapper mapper;

    public Boolean createEnterprise(EnterpriseCreateVO vo) {
        MemberEnterprise memberEnterprise = new MemberEnterprise();
        BeanUtils.copyProperties(vo, memberEnterprise);
        mapper.insert(memberEnterprise);
        return true;
    }

    public PageResult<EnterpriseVO> getEnterprisePage(EnterprisePageReqVO vo) {

        LambdaQueryWrapper<MemberEnterprise> wrapper = new LambdaQueryWrapper<MemberEnterprise>()
                .ge(ObjectUtils.isNotEmpty(vo.getBeginTime()), MemberEnterprise::getCreateTime, vo.getBeginTime())
                .le(ObjectUtils.isNotEmpty(vo.getEndTime()), MemberEnterprise::getCreateTime, vo.getEndTime())
                .like(StringUtils.isNotEmpty(vo.getName()),MemberEnterprise::getName,vo.getName());
        PageResult<MemberEnterprise> page = mapper.selectPage(vo, wrapper);
        List<EnterpriseVO> collect = page.getList().stream().map(m -> {
            EnterpriseVO enterpriseVO = new EnterpriseVO();
            BeanUtils.copyProperties(m, enterpriseVO);
            return enterpriseVO;
        }).collect(Collectors.toList());
        PageResult<EnterpriseVO> result = PageResult.empty();
        result.setList(collect);
        result.setTotal(page.getTotal());
        return result;
    }

    public EnterpriseInfoRespVO getEnterpriseInfoById(Long id) {
         MemberEnterprise memberEnterprise = mapper.selectById(id);
         EnterpriseInfoRespVO vo = new EnterpriseInfoRespVO();
         BeanUtils.copyProperties(memberEnterprise,vo);
         return vo;
    }
}
